package com.luca.sl651server.biz.service.impl;

import com.luca.sl651server.biz.service.BizService;
import com.luca.sl651server.constant.FrameConstant;
import com.luca.sl651server.enums.FrameFuncCodeEnum;
import com.luca.sl651server.util.DateUtil;
import com.luca.sl651server.util.HexStringUtil;
import com.luca.sl651server.dto.req.FrameReqWrapper;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBucket;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.StringCodec;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalUnit;
import java.util.Date;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class BizServiceImpl implements BizService {
    private static final Logger LOGGER = LoggerFactory.getLogger(BizServiceImpl.class);
    private static final SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final SimpleDateFormat sd2 = new SimpleDateFormat("yyyyMMddHHmm");

    private static final String CACHE_NAME_KTY = "sw:";

    @Resource
    RedissonClient redissonClient;

    @Override
    public boolean handler(FrameReqWrapper frameReqWrapper) {
        try {
           if( null !=  FrameFuncCodeEnum.getFrameFuncEnum(frameReqWrapper.getReqDto().getHeader().getFuncCode())) {
               LocalDateTime now = LocalDateTime.now();
               if(Objects.equals(FrameFuncCodeEnum.getFrameFuncEnum(frameReqWrapper.getReqDto().getHeader().getFuncCode()), FrameFuncCodeEnum.HEART_BEAT)) {
                   //链路维持
                   LOGGER.info("[缓存收到的链路维持数据]  frameReqWrapper is:{}", frameReqWrapper);
                   RBucket<String> rBucket = redissonClient.getBucket(CACHE_NAME_KTY + "status", StringCodec.INSTANCE);
                   rBucket.set(now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));//心跳时间
                   rBucket.expire(Duration.ofMinutes(5));//5分钟有效期 , 心跳包有效期
               }else {
                   //水位数据
                   LOGGER.info("[缓存收到的水位数据]  frameReqWrapper is:{}", frameReqWrapper);
                   Double swValue = frameReqWrapper.getReqDto().getBody().getElementMap().get(FrameConstant.BODY_ELEMENT_REVIER).get(0).getVal();
                   RMap<String, Object> rMap = redissonClient.getMap(CACHE_NAME_KTY + "data", StringCodec.INSTANCE);
                   rMap.put("sw", swValue);
                   rMap.put("cjsj", now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));//采集时间
                   rMap.expire(Duration.ofMinutes(120));//120分钟有效期，数据有效期

                   //有数据时表示链路也正常
                   RBucket<String> rBucket = redissonClient.getBucket(CACHE_NAME_KTY + "status", StringCodec.INSTANCE);
                   rBucket.set(now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));//心跳时间
                   rBucket.expire(Duration.ofMinutes(5));//5分钟有效期 , 心跳包有效期
               }
           }
        } catch (Exception e) {
            LOGGER.error("[缓存收到的水位数据]  frameReqWrapper is:{}", frameReqWrapper, e);
            return false;
        }
        return true;
    }


    public static void main(String[] args) throws ParseException {
        String date = "200605091535";
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    }
}
